package com.atguigu.eduservice.service.impl;

import com.atguigu.eduservice.controller.frontController.CourseFrontController;
import com.atguigu.eduservice.entity.*;
import com.atguigu.eduservice.entity.chapter.ChapterVo;
import com.atguigu.eduservice.entity.chapter.VideoVo;
import com.atguigu.eduservice.entity.vo.*;
import com.atguigu.eduservice.service.EduCourseService;
import com.atguigu.servicebase.exceptionhandler.GuliException;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.atguigu.eduservice.mapper.EduCourseMapper;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * @author cyt
 * @create 2020-05-07 10:33
 */
@Service
public class EduCourseServiceImpl extends ServiceImpl<EduCourseMapper, EduCourse> implements EduCourseService {
    @Autowired
    EduCourseMapper eduCourseMapper;

    @Autowired
    EduCourseDescriptionService eduCourseDescriptionService;
    @Autowired
    EduVideoService eduVideoService;
    @Autowired
    EduChapterService eduChapterService;

    @Override
    public String saveCourseInfo(CourseInfoForm courserInfoForm) {


        EduCourse eduCourse = new EduCourse();
        BeanUtils.copyProperties(courserInfoForm, eduCourse);

        boolean save = this.save(eduCourse);
        if (!save) {
            throw new GuliException(20001, "课程信息保存失败");
        }

        //保存课程详情信息
        EduCourseDescription description = new EduCourseDescription();
        description.setDescription(courserInfoForm.getDescription());
        description.setId(eduCourse.getId());

        boolean resultDescription = eduCourseDescriptionService.save(description);

        if (!resultDescription) {
            throw new GuliException(20001, "课程详情信息保存失败");
        }
        return eduCourse.getId();
    }

    // 根据查出 课程 id 得到它的全部信息
    @Override
    public List<ChapterVo> getAllinfo(String id) {
        // 差出全部的课程信息
        QueryWrapper<EduChapter> wrapper = new QueryWrapper<>();
        wrapper.eq("course_id", id);
        List<EduChapter> eduChapterList = eduChapterService.list(wrapper);
        // 差出全部的小结信息
        QueryWrapper<EduVideo> videowrapper = new QueryWrapper<>();
        videowrapper.eq("course_id", id);
        List<EduVideo> eduVideoList = eduVideoService.list(videowrapper);
        // 开始组合数据
        List<ChapterVo> chapterVoList = new ArrayList<>();

        for (EduChapter eduChapter : eduChapterList) {
            ChapterVo chapterVo = new ChapterVo();
            BeanUtils.copyProperties(eduChapter, chapterVo);


            ArrayList<VideoVo> videoVoArrayList = new ArrayList<>();

            for (EduVideo video : eduVideoList) {
                if (video.getChapterId().equals(chapterVo.getId())) {
                    VideoVo eduVideo = new VideoVo();
                    BeanUtils.copyProperties(video, eduVideo);
                    videoVoArrayList.add(eduVideo);
                }
            }
            chapterVo.setChildren(videoVoArrayList);
            chapterVoList.add(chapterVo);
        }
        return chapterVoList;
    }

    // id 查询出课程的信息
    @Override
    public CourseInfoForm getCourseById(String id) {
        System.out.println(id);
        QueryWrapper<EduCourse> wrapper = new QueryWrapper<>();
        wrapper.eq("id", id);
        EduCourse eduCourse = baseMapper.selectOne(wrapper);
        System.out.println(eduCourse);
        CourseInfoForm infoForm = new CourseInfoForm();
        BeanUtils.copyProperties(eduCourse, infoForm);
        EduCourseDescription descriptionServiceById = eduCourseDescriptionService.getById(id);
        infoForm.setDescription(descriptionServiceById.getDescription());
        return infoForm;
    }

    //更新
    @Override
    public String updateCourse(CourseInfoForm courserInfoForm) {

        EduCourse eduCourse = new EduCourse();
        BeanUtils.copyProperties(courserInfoForm, eduCourse);
        boolean b = this.saveOrUpdate(eduCourse);
        if (!b) {
            throw new GuliException(20001, "课程信息保存失败");
        }
        //保存课程详情信息
        EduCourseDescription description = new EduCourseDescription();
        description.setDescription(courserInfoForm.getDescription());
        description.setId(eduCourse.getId());

        boolean orUpdate = eduCourseDescriptionService.saveOrUpdate(description);

        if (!orUpdate) {
            throw new GuliException(20001, "课程详情信息保存失败");
        }
        return eduCourse.getId();
    }

    @Override
    public void pageQuery(Page<EduCourse> pageParam, CourseQuery courseQuery) {
        QueryWrapper<EduCourse> queryWrapper = new QueryWrapper<>();
        queryWrapper.orderByDesc("gmt_create");
        if (courseQuery == null) {
            baseMapper.selectPage(pageParam, queryWrapper);
            return;
        }
        String title = courseQuery.getTitle();
        String teacherId = courseQuery.getTeacherId();
        String subjectParentId = courseQuery.getSubjectParentId();
        String subjectId = courseQuery.getSubjectId();
        if (!StringUtils.isEmpty(title)) {
            queryWrapper.like("title", title);
        }
        if (!StringUtils.isEmpty(teacherId)) {
            queryWrapper.eq("teacher_id", teacherId);
        }
        if (!StringUtils.isEmpty(subjectParentId)) {
            queryWrapper.ge("subject_parent_id", subjectParentId);
        }
        if (!StringUtils.isEmpty(subjectId)) {
            queryWrapper.ge("subject_id", subjectId);
        }
        baseMapper.selectPage(pageParam, queryWrapper);
    }

    @Override
    public CoursePublishVo getCoursePublishVoById(String id) {

        return baseMapper.selectCoursePublishVoById(id);
    }

    @Override
    public Boolean publishCourseById(String id) {
        EduCourse course = new EduCourse();
        course.setId(id);
        course.setStatus("Normal");
        Integer count = baseMapper.updateById(course);
        return null != count && count > 0;
    }

    // 热门课程
    @Override
    public List<EduCourse> gethotCourse() {
        QueryWrapper<EduCourse> wrapper = new QueryWrapper<>();
        wrapper.orderByDesc("id");
        wrapper.last("limit 8");
        return baseMapper.selectList(wrapper);
    }

    // 条件查询带分页
    @Override
    public Map<String, Object> pageListWeb(Page<EduCourse> pageParam, CourseFrontVo courseFront) {
        QueryWrapper<EduCourse> queryWrapper = new QueryWrapper<>();

        if (courseFront == null) {
            baseMapper.selectPage(pageParam, queryWrapper);
            List<EduCourse> records = pageParam.getRecords();
            long current = pageParam.getCurrent();
            long pages = pageParam.getPages();
            long size = pageParam.getSize();
            long total = pageParam.getTotal();
            boolean hasNext = pageParam.hasNext();
            boolean hasPrevious = pageParam.hasPrevious();
            Map<String, Object> map = new HashMap<String, Object>();
            map.put("items", records);
            map.put("current", current);
            map.put("pages", pages);
            map.put("size", size);
            map.put("total", total);
            map.put("hasNext", hasNext);
            map.put("hasPrevious", hasPrevious);
            return map;
        }
        if (!StringUtils.isEmpty(courseFront.getSubjectParentId())) {
            queryWrapper.eq("subject_parent_id", courseFront.getSubjectParentId());
        }
        if (!StringUtils.isEmpty(courseFront.getSubjectId())) {
            queryWrapper.eq("subject_id", courseFront.getSubjectId());
        }
        if (!StringUtils.isEmpty(courseFront.getBuyCountSort())) {
            queryWrapper.orderByDesc("buy_count");
        }
        if (!StringUtils.isEmpty(courseFront.getGmtCreateSort())) {
            queryWrapper.orderByDesc("gmt_create");
        }
        if (!StringUtils.isEmpty(courseFront.getPriceSort())) {
            queryWrapper.orderByDesc("price");
        }
        baseMapper.selectPage(pageParam, queryWrapper);
        List<EduCourse> records = pageParam.getRecords();
        long current = pageParam.getCurrent();
        long pages = pageParam.getPages();
        long size = pageParam.getSize();
        long total = pageParam.getTotal();
        boolean hasNext = pageParam.hasNext();
        boolean hasPrevious = pageParam.hasPrevious();
        Map<String, Object> map = new HashMap<String, Object>();
        map.put("items", records);
        map.put("current", current);
        map.put("pages", pages);
        map.put("size", size);
        map.put("total", total);
        map.put("hasNext", hasNext);
        map.put("hasPrevious", hasPrevious);
        return map;
    }

    // 课程基本信息
    @Override
    public CourseWebVo getBaseInfo(String courseId) {
        return baseMapper.selectInfoWebById(courseId);
    }

    //  删除
    @Override
    public void delete(String id) {
        baseMapper.deleteById(id);
    }
}

